package com.isxcode.link.plugin.service;

import com.alibaba.fastjson.JSON;
import com.isxcode.link.common.constant.KafkaConfigConstants;
import com.isxcode.link.common.constant.UrlConstants;
import com.isxcode.link.common.properties.LinkPluginProperties;
import com.isxcode.link.common.pojo.LinkRequest;
import com.isxcode.link.common.pojo.LinkResponse;
import com.isxcode.link.common.exception.LinkException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 所有的异步服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LinkSyncService {

    private final LinkPluginProperties linkPluginProperties;

    private final KafkaTemplate<String, String> kafkaTemplate;

    private final LinkService linkService;

    @Async
    public void executeSyncWork(LinkRequest linkRequest, String url) {

        try {
            switch (url) {
                case UrlConstants.EXECUTE_URL:
                    execute(linkRequest);
                    break;
            }
        } catch (LinkException e) {
            log.debug(e.getCode() + e.getMsg() + e.getMessage());
            LinkResponse starResponse = new LinkResponse(e.getCode(), e.getMsg());
            kafkaTemplate.send(KafkaConfigConstants.DEFAULT_TOPIC_NAME, linkRequest.getExecuteId(), JSON.toJSONString(starResponse));
        }
    }

    public void execute(LinkRequest linkRequest) {


    }
}
